﻿<?php
/**
 * 定义如何处理题目的测试用例和正确测试结果请求
 *
 * @author kinet
 * @time 2010-9-20 2010-9-20
 * @version 1.0
 */
require_once ('File.php');
class problem_test_right_process {
	/**#@+
         * @access private
	 */

	/**
	 * 保存数据库连接.
	 * 
	 * @var object MDB2
	 */
	private $db ;
	
	/**
	 * 保存用户从页面提交而来的
	 * 信息.
	 * 
	 * @var array
	 */
	private $request ;

	/**
	 * 保存数据库中problem_test_right表对应的
	 * 对象.
	 * 
	 * @var object db_obj_problem_test_right
	 */
	private $db_obj_problem_test_right ;

	/**
	 *
	 * @access public
	 * @param MDB2 $db 
	 * @param array $string 
	 * @return bool
	 */
	function __construct( $db , $request=null ){
		
		$this->db = $db ;		
		$this->db_obj_problem_test_right = new db_obj_problem_test_right( $db );
		
		if ( !empty( $request ) ){
		
			$this->request = $request ;
			foreach( $request as $k => $v ){

				$this->db_obj_problem_test_right->set( $k , $v );
			}
		}

	}

	/**
	 * 委托db_obj_problem中的save()方法
	 *
	 * @access public
	 */
	function save(){
	    try{
			mkdir('F:\\Program Files\\EasyPHP 3.0\\www\\gearman\\test\\'.$this->db_obj_problem_test_right->get('problem_no'));
			mkdir('F:\\Program Files\\EasyPHP 3.0\\www\\gearman\\right\\'.$this->db_obj_problem_test_right->get('problem_no'));
		}
		catch(Exception $e){}
		for($i=0;$i<=9;$i++){
			try{
				
				
						
				$e = File::write( 'F:\\Program Files\\EasyPHP 3.0\\www\\gearman\\test\\'.$this->db_obj_problem_test_right->get('problem_no').'\\'.$i ,$this->db_obj_problem_test_right->get('test'.$i), FILE_MODE_WRITE);
				
			}
			catch(Exception $g){}
			
            try{			
		
			
				$f = File::write( 'F:\\Program Files\\EasyPHP 3.0\\www\\gearman\\right\\'.$this->db_obj_problem_test_right->get('problem_no').'\\'.$i ,$this->db_obj_problem_test_right->get('right'.$i), FILE_MODE_WRITE);
				
			}
			catch(Exception $h){}			
		}
		 $this->db_obj_problem_test_right->save() ;
		
	}	
	
	/**
	 * 委托db_obj_problem_test_right中的get()方法
	 *
	 * @access public
	 * @see db_obj
	 */
	function get( $k ){

		return $this->db_obj_problem_test_right->get( $k ) ;
	}

	/**
	 * 委托$b_obj_problem_test_right中的set()方法
	 * 
	 * @access public
	 * @see db_obj
	 */
	function set( $k , $v ){
      
		return $this->db_obj_problem_test_right->set( $k , $v ) ;
	}

	/**
	 * 委托db_obj_problem_test_right中的load()方法
	 *
	 * @access public
	 * @see db_obj
	 */
	function load( $k , $v ){
		
		return  $this->db_obj_problem_test_right->load( $k , $v ) ;
	}	
    function delete_by_no($no){
		$sql = "DELETE FROM problem_test_right WHERE problem_no =".$no ;
		$res = $this->db->query( $sql ); 
		if (PEAR::isError($res)) {
			throw new Exception( '删除出错'.$res->getMessage() );
		}
		try{
			
		 $this->delfile('F:\\Program Files\\EasyPHP 3.0\\www\\gearman\\test\\'.$this->db_obj_problem_test_right->get('problem_no'));
		 rmdir('F:\\Program Files\\EasyPHP 3.0\\www\\gearman\\test\\'.$this->db_obj_problem_test_right->get('problem_no'));
		 $this->delfile('F:\\Program Files\\EasyPHP 3.0\\www\\gearman\\right\\'.$this->db_obj_problem_test_right->get('problem_no'));
		 rmdir('F:\\Program Files\\EasyPHP 3.0\\www\\gearman\\right\\'.$this->db_obj_problem_test_right->get('problem_no'));
		 
		}
		catch(Exception $e){
		  echo $e;
		}
		
	  
	}
  
			//删除指定目录（文件夹）中的所有文件函数
			function delfile($dir) {
			if (is_dir($dir)) {
			  $dh=opendir($dir);//打开目录
			  //列出目录中的所有文件并去掉 . 和 ..
			  while (false !== ( $file = readdir ($dh))) {

				if($file!="." && $file!="..") { 
				  $fullpath=$dir."/".$file;
				  if(!is_dir($fullpath)) {
					  unlink($fullpath);//删除目录中的所有文件
				  } else {
					  delfile($fullpath);
				  }
				}
				closedir($dh);
			  }
			}
		}
	

}

